using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//Encoding：UTF-8
public class UIPlayer : MonoBehaviour
{
    public static UIPlayer singleton;
    
    public Pai paiPrefab;
    public RectTransform shouPaiArea;
    public RectTransform lastDrawPaiArea;
    public GameObject buttonParent;
    public Button buttonPlayPai;
    public Button buttonChi;
    public Button buttonPeng;
    public Button buttonGang;
    public Button buttonAnGang;
    public Button buttonJiaGang;
    public Button buttonTing;
    public Button buttonHu;
    public Button buttonZiMo;
    public Button buttonSkip;

    private List<Pai> paiGos = new List<Pai>();

    UIPlayer()
    {
        singleton = this;
    }

    private void Awake()
    {
        buttonPlayPai.onClick.AddListener(PlayPai);
        buttonChi.onClick.AddListener(Chi);
        buttonPeng.onClick.AddListener(Peng);
        buttonGang.onClick.AddListener(Gang);
        buttonAnGang.onClick.AddListener(AnGang);
        buttonJiaGang.onClick.AddListener(JiaGang);
        buttonTing.onClick.AddListener(Ting);
        buttonHu.onClick.AddListener(Hu);
        buttonZiMo.onClick.AddListener(ZiMo);
        buttonSkip.onClick.AddListener(Skip);
    }

    public void Init()
    {
        for (int i = 0; i < paiGos.Count; ++i)
        {
            Destroy(paiGos[i].gameObject);
        }
        paiGos.Clear();
        buttonParent.gameObject.SetActive(false);
    }

    #region 手牌的提示显示相关函数
    List<int> tipsShowIndexs = new List<int>();
    public void SetEffectTip(List<int> ids)
    {
        ClearEffectTip();
        tipsShowIndexs.Clear();
        for (int i = 0; i < ids.Count; ++i)
        {
            for (int j = 0; j < paiGos.Count; ++j)
            {
                if (!tipsShowIndexs.Contains(j) && paiGos[j].paiID == ids[i])
                {
                    tipsShowIndexs.Add(j);
                    paiGos[j].SetEffectTip(true);
                    break;
                }
            }
        }
    }

    private void ClearEffectTip()
    {
        for (int j = 0; j < paiGos.Count; ++j)
        {
            paiGos[j].SetEffectTip(false);
        }
    }
    #endregion

    #region 手牌的显示相关函数
    private void SortPai()
    {
        for (int i = 0; i < paiGos.Count - 1; ++i)
        {
            for (int j = i + 1; j < paiGos.Count; ++j)
            {
                if (paiGos[i].paiID > paiGos[j].paiID)
                {
                    Pai p = paiGos[i];
                    paiGos[i] = paiGos[j];
                    paiGos[j] = p;
                }
            }
            paiGos[i].transform.SetAsLastSibling();
        }
        paiGos[paiGos.Count - 1].transform.SetAsLastSibling();
    }

    Pai lastDrawPai;
    public void AddPai(int id)
    {
        lastDrawPaiArea.gameObject.SetActive(true);
        lastDrawPai = Instantiate(paiPrefab, lastDrawPaiArea);
        lastDrawPai.SetPaiType(id);
        paiGos.Add(lastDrawPai);
    }

    public void AddHand(int[] ids)
    {
        for (int i = 0; i < ids.Length; ++i)
        {
            Pai p = Instantiate(paiPrefab, shouPaiArea);
            p.SetPaiType(ids[i]);
            paiGos.Add(p);
        }
        SortPai();
    }

    public void DeletePai(Pai pai)
    {
        Destroy(pai.gameObject);
        paiGos.Remove(pai);
        OrganizePai();
    }

    public void DeletePais(List<int> ids)
    {
        for (int i = 0; i < ids.Count; ++i)
        {
            int j = 0;
            for (; j < paiGos.Count; ++j)
            {
                if (paiGos[j].paiID == ids[i])
                {
                    Destroy(paiGos[j].gameObject);
                    break;
                }
            }
            paiGos.RemoveAt(j);
        }
        OrganizePai();
    }

    private void OrganizePai()
    {
        if (lastDrawPai != null)
        {
            lastDrawPaiArea.gameObject.SetActive(false);
            lastDrawPai.transform.parent = shouPaiArea;
            lastDrawPai = null;
            SortPai();
        }
    }
    #endregion

    #region 玩家操作按钮的显隐函数
    public void SetPlayerButtonShow(bool buttonPlayPaiShow, bool buttonChiShow, bool buttonPengShow, bool buttonGangShow, bool buttonAnGangShow, bool buttonJiaGangShow, bool buttonTingShow, bool buttonHuShow, bool buttonZiMoShow, bool buttonSkipShow)
    {
        buttonParent.gameObject.SetActive(true);
        buttonPlayPai.gameObject.SetActive(buttonPlayPaiShow);
        buttonChi.gameObject.SetActive(buttonChiShow);
        buttonPeng.gameObject.SetActive(buttonPengShow);
        buttonGang.gameObject.SetActive(buttonGangShow);
        buttonAnGang.gameObject.SetActive(buttonAnGangShow);
        buttonJiaGang.gameObject.SetActive(buttonJiaGangShow);
        buttonTing.gameObject.SetActive(buttonTingShow);
        buttonHu.gameObject.SetActive(buttonHuShow);
        buttonZiMo.gameObject.SetActive(buttonZiMoShow);
        buttonSkip.gameObject.SetActive(buttonSkipShow);
    }
    #endregion

    #region 玩家操作按钮触发的事件
    public void PlayPai()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.PlayPai);
        buttonParent.gameObject.SetActive(false);
    }

    public void Chi()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.Chi);
        buttonParent.gameObject.SetActive(false);
        ClearEffectTip();
    }

    public void Peng()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.Peng);
        buttonParent.gameObject.SetActive(false);
        ClearEffectTip();
    }

    public void Gang()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.Gang);
        buttonParent.gameObject.SetActive(false);
        ClearEffectTip();
    }

    public void AnGang()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.AnGang);
        buttonParent.gameObject.SetActive(false);
        ClearEffectTip();
    }

    public void JiaGang()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.JiaGang);
        buttonParent.gameObject.SetActive(false);
        ClearEffectTip();
    }

    public void Ting()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.Ting);
        buttonParent.gameObject.SetActive(false);
    }

    public void Hu()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.Hu);
        buttonParent.gameObject.SetActive(false);
    }

    public void ZiMo()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.ZiMo);
        buttonParent.gameObject.SetActive(false);
    }

    public void Skip()
    {
        PaiJuManager.singleton.GetPlayerController().SetAction(PlayerBase.Action.Skip);
        buttonParent.gameObject.SetActive(false);
        ClearEffectTip();
    }
    #endregion
}
